#! /bin/bash

if [ $EUID -ne 0 ]; then
    echo "$(basename $0) must be run as root"
    exit 1
fi

# Check if script is being ran frmo the install media
if [ -d /tmp/frzr_root ]; then
    
    if [ -d "${SUBVOL}" ]; then
        
        cd ${SUBVOL}
        # Mount necessary file systems
        mount -t proc /proc proc/
        mount -t sysfs /sys sys/
        mount --rbind /dev dev/
        
        # Set R/W permissions
        btrfs property set -fts ${SUBVOL} ro false
	chroot ${SUBVOL} /bin/bash <<EOF
# Commands to be ran in chroot

### Create distro mkinitcpio preset
echo '
ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux"
ALL_microcode=(/boot/*-ucode.img)

PRESETS="default"

default_image="/boot/initramfs-linux.img"
' > /etc/mkinitcpio.d/\${NAME%%-*}.preset

### Rebuild Initramfs with custom preset
mkinitcpio -p \${NAME%%-*}
EOF
        # Set back to R/O permissions
        btrfs property set -fts ${SUBVOL} ro true
	# unmount filesystems
	umount -l ${SUBVOL}/proc
    	umount -l ${SUBVOL}/sys
    	mount --make-rslave ${SUBVOL}/dev
    	umount -l ${SUBVOL}/dev
    else
        echo "No deployment directory found"
        exit 1
    fi
else
    echo "We don't appear to be running from an arch install media"
fi


# Build initramfs from within a deployed system
if [ -d /frzr_root ]; then

    if [ -n "$SUBVOL" ]; then
        ### Grabbing name of the new deployed system
        ID=$(grep '^ID=' "$SUBVOL/etc/os-release" | awk -F= '{ print $2 }' | sed 's/"//g')
        VERSIONID=$(grep '^VERSION_ID=' "$SUBVOL/etc/os-release" | awk -F= '{ print $2 }' | sed 's/"//g')
        BUILDID=$(grep '^BUILD_ID=' "$SUBVOL/etc/os-release" | awk -F= '{ print $2 }' | sed 's/"//g')
    else
        ### Grabbing name of the currently deployed system
        ID=$(grep '^ID=' /etc/os-release | awk -F= '{ print $2 }' | sed 's/"//g')
        VERSIONID=$(grep '^VERSION_ID=' /etc/os-release | awk -F= '{ print $2 }' | sed 's/"//g')
        BUILDID=$(grep '^BUILD_ID=' /etc/os-release | awk -F= '{ print $2 }' | sed 's/"//g')
    fi

    BUILD="$ID"-"$VERSIONID"_"$BUILDID"
    DEPLOYMENT_PATH="/frzr_root/deployments/$BUILD"
    
    # Get locked state
    RELOCK=0
    LOCK_STATE=$(btrfs property get -fts "$DEPLOYMENT_PATH")
    if [[ $LOCK_STATE == *"ro=true"* ]]; then
        btrfs property set -fts ${DEPLOYMENT_PATH} ro false
        RELOCK=1
    else
        echo "Filesystem appears to be unlocked"
    fi
    
    echo "Generating configuration for $BUILD"
    
    ### Rebuild Initramfs with custom preset
    
    if [ -n "$SUBVOL" ]; then
        cd ${SUBVOL}
        # Mount necessary file systems
        mount -t proc /proc proc/
        mount -t sysfs /sys sys/
        mount --rbind /dev dev/

	# We have to chroot for new images or else the kernel version can cause initramfs building to fail
	chroot ${SUBVOL} /bin/bash <<EOF
# Commands to be ran in chroot
### Create distro mkinitcpio preset
echo '
ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux"
ALL_microcode=(/boot/*-ucode.img)

PRESETS="default"

default_image="/boot/initramfs-linux.img"
' > /etc/mkinitcpio.d/\${NAME%%-*}.preset

### Rebuild Initramfs with custom preset
mkinitcpio -p \${NAME%%-*}
EOF
    umount -l ${SUBVOL}/proc
    umount -l ${SUBVOL}/sys
    mount --make-rslave ${SUBVOL}/dev
    umount -l ${SUBVOL}/dev
    else
       echo '
ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/'$BUILD'/vmlinuz-linux"
ALL_microcode=(/boot/'$BUILD'/*-ucode.img)

PRESETS="default"

default_image="/boot/'$BUILD'/initramfs-linux.img"
' > /etc/mkinitcpio.d/$ID.preset
       # If we are not doing a deployment then this will be used for local installs to rebuild initramfs
       mkinitcpio -p $ID
    fi
    
    if [[ $RELOCK == 1 ]]; then
       btrfs property set -fts ${DEPLOYMENT_PATH} ro true
    else
       # Move rebuilt images to the unlocked location if system was unlocked prior
       cp /boot/$BUILD/* /boot
    fi
fi
